home *** CD-ROM | disk | FTP | other *** search
/ Atari Forever 4 / Atari Forever 4.zip / Atari Forever 4.iso / SERIE_AI / AI_079 / PAULA25 / UTILS / MP.C < prev    next >
C/C++ Source or Header  |  1998-03-14  |  5KB  |  170 lines

  1. /* tabsize = 4
  2.  *
  3.  * ModulePlayer-Interface: MP.PRG
  4.  * ------------------------------
  5.  *
  6.  * Purpose:                Access PAULA from command shells!
  7.  *
  8.  * Usage:                MP <filename> [<filename> ...]
  9.  *                        MP STOP            (stop player)
  10.  *                        MP KILL            (shutdown player)
  11.  *                        MP                 (TOP window)
  12.  *
  13.  * Program description:
  14.  *    MP reads its command line, evaluates the parameters and sends an
  15.  *  MP_START-message to PAULA. PAULA must be in the system either as ACC or
  16.  *  PRG application.
  17.  *
  18.  * Operating Systems:    TOS, MultiTOS, Mag!X
  19.  *                        Geneva (untested)
  20.  * !! Attention: under MultiTOS with memory protection make sure that the
  21.  * !! memory of MP is declared as 'readable' (shared memory!!!!)
  22.  *
  23.  * Status:                This sample code is public domain. If you do changes
  24.  *                        to it, please document them!
  25.  *
  26.  * Version    Date        Author                Comment
  27.  * -----------------------------------------------------------------------
  28.  * REV 1.00    15-sep-1994    P. Fellerich        Original version.
  29.  *
  30.  */
  31.  
  32. #include <stdio.h>
  33. #include <aes.h>
  34. #include <tos.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37.  
  38.  
  39. /*
  40.  * VA_START: Activates an accessory. Word 3 + 4 contain a pointer to a
  41.  *    command string (may also be a NULL pointer!)
  42.  *  This command string contains file specifications, separated by
  43.  *    either spaces, commas or semicolons.
  44.  */
  45. #define VA_START            0x4711
  46.  
  47. /* MP_ACK: Paula says: OK! The previous command has been understood.
  48.  *    In case of a MP_START message, the sender may destroy his copy of the
  49.  *    command string.
  50.  *    In addition, msgbuf[7] contains the version number of Paula:
  51.  *    0x0204 means V 2.4
  52.  */
  53. #define MP_ACK            0x4800
  54.  
  55. /* MP_NAK: Paula says: NOT OK. The command was understood but rejected.
  56.  *    msgbuf[7] contains the version number
  57.  */
  58. #define MP_NAK            0x4801
  59.  
  60. /* MP_START: identical to VA_START, except for:
  61.  *    1. the message number is a different one.
  62.  *  2. After having read and evaluated the command string, PAULA replies
  63.  *       to the sender with either MP_ACK (success) or MP_NAK (failure)
  64.  */
  65. #define MP_START        0x4802
  66.  
  67. /* MP_STOP: Stops the player and frees the memory taken by the MOD file.
  68.  *    The main window of Paula stays open.
  69.  *  Upon reception of this command Paula replies with MP_ACK
  70.  */
  71. #define MP_STOP            0x4803
  72.  
  73. /* MP_SHUTDOWN: Stop player, release memory, close window. In PRG mode,
  74.  *  Paula terminates then.
  75.  *  Upon reception of this command Paula replies with MP_ACK.
  76.  */
  77. #define MP_SHUTDOWN        0x4804        /* total shut down */
  78.  
  79.  
  80. /* ==================================================================== */
  81. int        gl_apid=-1;
  82. int     msgbuf[8];
  83. char    *pcmd, extend[256];
  84. /* ==================================================================== */
  85.  
  86.  
  87. /* send a message to PAULA and wait for handshake message.
  88.  */ 
  89. int send_paula_message(int rx_id, int msg, int *mbuf)
  90. {
  91.     int        ebuf[8];
  92.     int        evnt, d;
  93.     
  94.     mbuf[0] = msg;
  95.     mbuf[1] = gl_apid;
  96.     mbuf[2] = 0;
  97.     appl_write(rx_id, 16, mbuf);        /* send action */
  98.  
  99.     /* now wait for the answer: */
  100.     evnt = evnt_multi(
  101.             MU_MESAG|MU_TIMER,0,0,0,        0,0,0,0,
  102.             0,0,0,0,                        0,0,ebuf,5000,
  103.             0,&d,&d,&d,                        &d,&d,&d );
  104.  
  105.     /* we've got an answer: */
  106.     if (evnt & MU_MESAG)
  107.     {
  108.         if (ebuf[1]==rx_id && (ebuf[0]==MP_ACK || ebuf[0]==MP_NAK))
  109.             return 0;            /* OK */
  110.         else return -1;            /* error! */
  111.     }
  112.     return -1;                    /* timeout! */
  113. }
  114.  
  115.  
  116. /* main: everything else.
  117.  */
  118. int main(int argc, char *argv[])
  119. {
  120.     int     i, paula_id;
  121.     char    *tmp;
  122.     
  123.     gl_apid=appl_init();                /* say hello to GEM */
  124.     if (gl_apid<0) return(0);
  125.     
  126.     paula_id=appl_find("PAULA   ");        /* search for PAULA */
  127.     if (paula_id<0) goto hell;
  128.     
  129.     if (argc>1)                            /* do we have a commandline? */
  130.     {
  131.         if (argc==2 && stricmp(argv[1],"STOP")==0)
  132.                 send_paula_message(paula_id, MP_STOP, msgbuf);
  133.         else if (argc==2 && stricmp(argv[1],"KILL")==0)
  134.                 send_paula_message(paula_id, MP_SHUTDOWN, msgbuf);
  135.         else
  136.         {
  137.             for(pcmd="\0", i=1; i<argc; i++)
  138.             {
  139.                 strcpy(extend,argv[i]);
  140.                 shel_find(extend);
  141.                 tmp=malloc(strlen(pcmd)+(strlen(extend)+1L));
  142.                 if (tmp!=NULL)
  143.                 {
  144.                     strcpy(tmp, pcmd);
  145.                     if (i>1) strcat(tmp, ";");
  146.                     strcat(tmp, extend);
  147.                     free(pcmd);
  148.                     pcmd=tmp; tmp=NULL;
  149.                 };
  150.             };
  151.             *(char **)&msgbuf[3] = pcmd;
  152.             /* only for VA_START: */
  153.             /* msgbuf[6]=1; msgbuf[7]=0; */
  154.             send_paula_message(paula_id, MP_START, msgbuf);
  155.             free(pcmd);
  156.         };
  157.     }
  158.     else                    /* no command line -> call PAULA */
  159.     {
  160.         *(char **)&msgbuf[3] = NULL;
  161.         send_paula_message(paula_id, MP_START, msgbuf);
  162.     }
  163. hell:
  164.     appl_exit();
  165.     return(0);
  166. }
  167.  
  168.  
  169. /* ================= eof. =========================================== */
  170.